package cn.cdeden.knowledge.service.impl;


import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.knowledge.domain.SysPortalConfigure;
import cn.cdeden.knowledge.domain.dto.EnterpriseFilesTreeDto;
import cn.cdeden.knowledge.service.SysPortalConfigureService;
import cn.cdeden.knowledge.service.dto.SysPortalConfigureDto;
import cn.cdeden.knowledge.service.dto.SysPortalConfigureQueryParam;
import cn.cdeden.knowledge.service.mapper.SysPortalConfigureMapper;
import cn.cdeden.knowledge.utils.PageUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author gjx
* @date 2024-09-24
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = SysPortalConfigureService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysPortalConfigureServiceImpl extends ServiceImpl<SysPortalConfigureMapper, SysPortalConfigure> implements SysPortalConfigureService {

    // private final RedisUtils redisUtils;
    private final SysPortalConfigureMapper sysPortalConfigureMapper;

    @Override
    public TableDataInfo<SysPortalConfigure> queryAll(SysPortalConfigureQueryParam query, Pageable pageable) {
        IPage<SysPortalConfigure> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<SysPortalConfigure> page = sysPortalConfigureMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return TableDataInfo.build(page);
    }

    @Override
    public List<SysPortalConfigureDto> queryAll(SysPortalConfigureQueryParam query){
        return MapstructUtils.convert(sysPortalConfigureMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), SysPortalConfigureDto.class);
    }

    @Override
    public SysPortalConfigure getById(Long id) {
        return sysPortalConfigureMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public SysPortalConfigureDto findById(Long id) {
        return MapstructUtils.convert(getById(id), SysPortalConfigureDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insert(SysPortalConfigureDto resources) {
        SysPortalConfigure entity = MapstructUtils.convert(resources, SysPortalConfigure.class);
        return this.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(SysPortalConfigureDto resources){
        SysPortalConfigure entity = MapstructUtils.convert(resources, SysPortalConfigure.class);
        boolean ret = this.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(Set<Long> ids){
        // delCaches(ids);
        return this.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id){
        Set<Long> set = new HashSet<>(1);
        set.add(id);
        return this.deleteByIds(set);
    }

    /*
    private void delCaches(Long id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Long> ids) {
        for (Long id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<SysPortalConfigureDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (SysPortalConfigureDto sysPortalConfigure : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("图标", sysPortalConfigure.getIcon());
              map.put("名称", sysPortalConfigure.getName());
              map.put("配置文件的ID", sysPortalConfigure.getFileId());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
